package org.redr.widgetGUI.core;

import java.io.File;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;

import com.trolltech.qt.core.QPoint;
import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.core.Qt.MouseButton;
import com.trolltech.qt.gui.QAction;
import com.trolltech.qt.gui.QGraphicsPixmapItem;
import com.trolltech.qt.gui.QGraphicsRectItem;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QGraphicsView;
import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QMenu;
import com.trolltech.qt.gui.QMouseEvent;
import com.trolltech.qt.gui.QPixmap;

public class GraphicsView extends WidgetState{

	private WidgetLabel		label;
	private QGraphicsView	view;
	private QGraphicsScene	scene;
	private	QGraphicsPixmapItem	mainItem;
	private QMenu menu;
	private QGraphicsRectItem	widgetSelectionRect;
	private QPointF mouseDownPosition; 
	
	public GraphicsView(LayoutState parent, WidgetSettings ws) {
		super(parent, ws);
		this.label	=	new WidgetLabel(this.layout, ws);
		this.view	=	new QGraphicsView();
		this.layout.addWidget(this.view);
		this.scene	=	new QGraphicsScene();
		this.view.setScene(this.scene);
		this._loadSettings();
	}
	
	public GraphicsView(QLayout layout, WidgetSettings ws){
		super(layout, ws);
		this.label	=	new WidgetLabel(this.layout, ws);
		this.view	=	new QGraphicsView(this.widgetBox);
		this.scene	=	new QGraphicsScene();
		this.view.setScene(this.scene);
		this._loadSettings();
	}
	
	protected void _loadSettings(){
		// ### right click menu
		this.menu = new QMenu(this.view);
//        QMenu save = this.menu.addMenu(_("Save As"));
//        save.addAction(_("Bitmap"));
//        save.addAction(_("PDF"));
//        save.addAction(_("Post Script"));
//        save.addAction(_("JPEG"));
//        this.menu.addAction(_("Copy"));
        this.menu.addAction(_("Fit In Window"));
        this.menu.addAction(_("Zoom Out"));
        this.menu.addAction(_("Zoom In"));
        
		if (!this.settings.getSetting("image").equals("")){
			if (this.settings.getSetting("imageType").toLowerCase().equals("png")){
				this.loadPNG(this.settings.getSetting("image"));
			}
		}
	}
	
	public void loadPNG(File f){
		loadPNG(f.getAbsolutePath());
	}
	
	public void loadPNG(String s){
		this.settings.setSetting("image", s);
//		if imageType == "svg":
//            #this.convertSVG(unicode(os.path.join(redREnviron.directoryNames["tempDir"], image)).replace("\\", "/")) ## handle the conversion to glyph free svg
//            mainItem = QGraphicsSvgItem(unicode(os.path.join(redREnviron.directoryNames["tempDir"], image)).replace("\\", "/"))
//        #else if imageType == "cairo":
//            #print "Executing cairo plotting"
//            #this.convertSVG(unicode(os.path.join(redREnviron.directoryNames["tempDir"], image)).replace("\\", "/")) ## handle the conversion to glyph free svg
//            #mainItem = QGraphicsSvgItem(unicode(os.path.join(redREnviron.directoryNames["tempDir"], image)).replace("\\", "/"))
        // else if imageType in ["png", "jpeg"]:
        // mainItem = QGraphicsPixmapItem(QPixmap(OSPath.join(redREnviron.directoryNames["tempDir"], image.replace("\\", "/"))))
        this.mainItem	=	new QGraphicsPixmapItem( new QPixmap(s) );
//		else:
//            raise Exception, "Image type %s not specified in a plotting method" % imageType
        this.scene.addItem(mainItem);
        // this.mainItem = mainItem
        this.view.update();
	}
	
	public void clear(){
		this.settings.setSetting("image", "");
		this.scene.clear();
		this.scene.update();
	}
	
	protected void mousePressEvent(QMouseEvent mouseEvent){
	       if (mouseEvent.button().equals(MouseButton.RightButton)) {
	            // # remove the selection rect
	            if (this.widgetSelectionRect != null){
	                this.widgetSelectionRect.hide();
	                this.widgetSelectionRect = null;
	            }
	            // # show the action menu
	            QPoint newCoords = mouseEvent.globalPos();
	            QAction action = this.menu.exec(newCoords);
	            if ( action != null ){
	            	
	                if ( action.text().equals(_("Zoom Out"))) 
	                	this.view.scale(0.80, 0.80);
	                else if ( action.text().equals(_("Zoom In"))) 
	                	this.view.scale(1.50, 1.50);
	                else if ( action.text().equals(_("Fit In Window"))){
	                	if ( this.mainItem != null ) 
	                		this.view.fitInView(this.mainItem.boundingRect(), Qt.AspectRatioMode.KeepAspectRatio);
	                }
	            }
	       } else {
	            this.mouseDownPosition = this.view.mapToScene(mouseEvent.pos());
	            this.widgetSelectionRect = new QGraphicsRectItem( new QRectF(this.mouseDownPosition, this.mouseDownPosition), null, this.scene);
	            this.widgetSelectionRect.setZValue(-100);
	            this.widgetSelectionRect.show();
	       }
	}
	
	protected void mouseMoveEvent(QMouseEvent ev){
	        try{
	            QPointF point = this.view.mapToScene(ev.pos());

	            if ( this.widgetSelectionRect != null )  this.widgetSelectionRect.setRect( new QRectF(this.mouseDownPosition, point));            

	            this.scene.update();
	        } catch (Exception e) {
	        	e.printStackTrace();
	        }
	}
	        
    protected void mouseReleaseEvent(QMouseEvent ev){
	        // QPointF point = this.view.mapToScene(ev.pos());
	        if ( this.widgetSelectionRect != null ){
	            this.view.fitInView(this.widgetSelectionRect.rect(), Qt.AspectRatioMode.KeepAspectRatio);
	            this.widgetSelectionRect.hide();
	            this.widgetSelectionRect = null;
	        }
	        this.scene.update();
    }

}
